<template>
  <div>
    <f-search-form :more='true' @searchHandler='searchHandler' @exportHandler='exportHandler' :show-export-btn='true'>
      <f-search-form-item label='单据编号' name='orderNo' />
      <f-search-form-item label='单位名称' name='customName' />
      <f-search-form-item label='业务员' name='saleManName'  />
      <f-search-form-item label='部门' ref='dept' name='deptId' type='select' slot='advanced' :data='api.hyorder.listByCompanyId' valueKey='deptId' label-key='deptName' />
      <f-search-form-item label='单据日期' type='range' name='beginDate' slot='advanced' />
      <f-search-form-item label='制单人' name='nickName' slot='advanced' />
    </f-search-form>
    <f-table ref='table' rowKey='orderId'
             :tableColumns='columns' 
             :scroll='{x:true}' 
             :multiple='false' 
             :operator='operator' 
             :params='queryParam' 
             :api='api.hyorder' 
             handler='hyOrderTab2ListHandler'>
      <span slot='sendType' slot-scope='text, record, index'>
        <a-tag color='#2db7f5' v-if='text==1'>现款</a-tag>
        <a-tag color='#FF0000' v-if='text==2'>预收款</a-tag>
        <a-tag color='#87d068' v-if='text==3'>资保金</a-tag>
        <a-tag color='purple' v-if='text==4'>全额资信</a-tag>
      </span>
      <span slot='deptType' slot-scope='text, record, index'>
        <a-tag color='#87d068' v-if='text=="ZS"'>招商</a-tag>
        <a-tag color='#108ee9' v-if='text=="ZY"'>自营</a-tag>
      </span>
      <span slot='sendState' slot-scope='text, record, index'>
        <a-tag color='#87d068' v-if='text==0'>未提单</a-tag>
        <a-tag color='purple' v-if='text==1'>已提单</a-tag>
      </span>
      <span slot='invoiceType' slot-scope='text, record, index'>
            <a-tag color='#87d068' v-if='text==1'>普票</a-tag>
            <a-tag color='purple' v-if='text==2'>专票</a-tag>
        </span>
      <span slot='printTag' slot-scope='text, record, index'>
         <a-tag color='#87d068' v-if='text==0'>未打印</a-tag>
         <a-tag color='purple' v-if='text>=1'>已打印</a-tag>
      </span>
      <span slot='action' slot-scope='text, record'>
          <template>
            <a href='javascript:;' @click='$refs.tab2DetailsDrawer.showDrawer(record)'>详情</a>
              <a-divider type='vertical' v-if='record.sourceType==1'/>
              <a v-if='record.sourceType==1' @click='$refs.drawer.showDrawer(record)'>编辑</a>
              <a-divider type='vertical' />
          </template>
          <a-dropdown>
            <a class='ant-dropdown-link'>更多<a-icon type='down' /></a>
            <a-menu slot='overlay'>
              <a-menu-item v-if='record.orderState==2&& (record.sendType==3 ||record.sendType==4 )'>
                <a href='javascript:;' @click='syncOrderReturn(record)'>同步到待回款</a>
              </a-menu-item>
              <!--未提单,已提交审批-->
              <a-menu-item v-if='record.sendState==0 && record.orderState==1 && record.revokeState==0'>
                <a href='javascript:;' @click='revokeProcess(record)'>申请退回</a>
              </a-menu-item>
               <a-menu-item v-if='record.sendState==0 && record.orderState==1 && record.revokeState==1'>
                <a href='javascript:;' @click='cancelRevokeProcess(record)'>撤销退回申请</a>
              </a-menu-item>
              <a-menu-item v-if='record.orderState!=0'>
                 <a href='javascript:;' @click='processImg(record)'>审批历史</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='$refs.gxhtModal.open(record)'>下载购销合同</a>
              </a-menu-item>
              <a-menu-item v-if='record.companyId==1018 && record.customName=="湖北丹博瑞医药有限公司"'>
                <a href='javascript:;' @click='downloadGxhtCg(record)'>下载购销合同(采购)</a>
              </a-menu-item>
               <a-menu-item v-if='record.sendState==1'>
                <a href='javascript:;' @click='$refs.backDrawer.showDrawer(record)'>申请退货</a>
              </a-menu-item>
              <a-menu-item>
                <a @click='$refs.tuibuDrawer.showDrawer(record)'>退补差价</a>
              </a-menu-item>
              <a-menu-item>
                <a href='javascript:;' @click='printOrder(record)'>发货单</a>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
    </f-table>
    <hy-order-drawer ref='drawer' @success='onLoad'></hy-order-drawer>
    <tuibu-drawer ref='tuibuDrawer'></tuibu-drawer>
    <hy-order-tab2-details-drawer ref='tab2DetailsDrawer' @success='changeHandler'></hy-order-tab2-details-drawer>
    <process-image ref='processImg' v-if='processImageVisible' :visible.sync='processImageVisible' :processInstanceId='processInstanceId'></process-image>
    <print-modal ref='printModal'></print-modal>
    <gxht-modal ref='gxhtModal'></gxht-modal>
    <back-order-modal ref='backOrderModal' @success='changeHandler'></back-order-modal>
    <back-drawer ref='backDrawer'></back-drawer>
  </div>
</template>
<script>
import api from "../js/api"
import HyOrderDrawer from "./hy_order_drawer"
import ProcessImage from "../../flowable/process/process_image"
import PrintModal from "@/views/module/haiying/print/print_modal"
import GxhtModal from "./components/gxht_modal"
import BackOrderModal from "@/views/module/haiying/order/components/back_order_modal"
import HyOrderTab2DetailsDrawer from "@/views/module/haiying/order/details/hyorder_tab2_details_drawer"
import TuibuDrawer from "@/views/module/haiying/order/tuibu/tuibu_drawer"
import BackDrawer from "@/views/module/haiying/order/back/back_drawer"

export default {
  name: "Tab2",
  components: {
    BackDrawer,
    TuibuDrawer,
    BackOrderModal,
    PrintModal,
    HyOrderDrawer,
    HyOrderTab2DetailsDrawer,
    GxhtModal,
    ProcessImage
  },
  data() {
    let me = this
    //操作按钮
    let operator = []
    return {
      api: Object.assign(api, this.api),
      //列表初始化查询参数
      queryParam: {
        fileName: "订单明细-",
      },
      activeKey: "0",
      // 表头
      columns: [
        {
          title: "序号",
          width: "60px",
          align: "center",
          fixed: "left",
          customRender: (text, record, index) => index + 1
        },
        {
          title: "订单编号",
          align: "center",
          fixed: "left",
          onCopy: true,
          dataIndex: "orderNo"
        },
        {
          title: "发货单号",
          align: "center",
          onCopy: true,
          dataIndex: "outOrderNo"
        },
        {
          title: "发货日期",
          align: "left",
          width: "100px",
          dataIndex: "sendDate"
        },
        {
          title: "发货方式",
          align: "left",
          dataIndex: "sendType",
          width: "100px",
          scopedSlots: { customRender: "sendType" }
        },
        {
          title: "部门",
          align: "left",
          width: "130px",
          dataIndex: "deptName"
        },
        {
          title: "业务类型",
          align: "center",
          width: "80px",
          dataIndex: "deptType",
          scopedSlots: { customRender: "deptType" }
        },
        {
          title: "发货区域",
          align: "left",
          width: "80px",
          dataIndex: "sendArea"
        },
        {
          title: "单位名称",
          align: "left",
          ellipsis: true,
          length: 25,
          width: "130px",
          dataIndex: "customName"
        },
        {
          title: "业务员",
          align: "left",
          width: "120px",
          ellipsis: true,
          length: 15,
          dataIndex: "saleManName"
        },
        {
          title: "业务编码",
          align: "left",
          width: "110px",
          ellipsis: true,
          length: 22,
          dataIndex: "businessCode"
        },
        {
          title: "发货金额",
          align: "center",
          width: "100px",
          dataIndex: "totalSaleAmount"
        },
        {
          title: "单据日期",
          align: "left",
          width: "110px",
          dataIndex: "createDate"
        },
        {
          title: "提单状态",
          align: "center",
          isColumnHidden:true,
          width: "100px",
          dataIndex: "sendState",
          scopedSlots: { customRender: "sendState" }
        },
        {
          title: "发货物流公司",
          align: "left",
          width: "110px",
          dataIndex: "fastMailName"
        },
        {
          title: "发货物流单号",
          align: "left",
          width: "110px",
          dataIndex: "fastMailNo"
        },
        {
          title: "发票类型",
          align: "left",
          dataIndex: "invoiceType",
          scopedSlots: { customRender: "invoiceType" }
        },
        {
          title: "开票要求",
          align: "left",
          ellipsis: true,
          dataIndex: "invoiceRemark"
        },
        {
          title: "发票号码",
          align: "left",
          width: "110px",
          dataIndex: "invoiceNo"
        },
        {
          title: "发票日期",
          align: "left",
          width: "110px",
          dataIndex: "invoiceDate"
        },
        {
          title: "发票物流公司",
          align: "left",
          width: "110px",
          dataIndex: "invoiceFastMailName"
        },
        {
          title: "发票物流单号",
          align: "left",
          width: "110px",
          dataIndex: "invoiceFastMailNo"
        },
        {
          title: "制单人",
          align: "center",
          width: "120px",
          dataIndex: "nickName"
        },
        {
          title: "打印状态",
          align: "center",
          width: "100px",
          fixed: "right",
          isColumnHidden:true,
          dataIndex: "printTag",
          scopedSlots: { customRender: "printTag" }
        },
        {
          title: "操作",
          align: "center",
          dataIndex: "action",
          width: "150px",
          fixed: "right",
          scopedSlots: { customRender: "action" }
        }
      ],
      operator: operator,
      processImageVisible: false,
      processInstanceId: null
    }
  },
  created() {
  },
  methods: {
    onLoad() {
      this.$refs.table.refresh()
    },
    printOrder(record) {
      this.$refs["printModal"].open(record.orderId, 1)
    },
    //申请取回订单
    revokeProcess(order) {
      this.http.confirm(this, {
        content: "确认申请取回订单吗?",
        url: api.hyorder.revokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //同步订单明细到回款表
    syncOrderReturn(order) {
      this.http.confirm(this, {
        content: "确认同步订单明细到回款表吗?",
        url: api.hyorder.syncOrderReturn,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    //申请撤销审批
    cancelRevokeProcess(order) {
      this.http.confirm(this, {
        content: "确认取消申请撤销审批吗?",
        url: api.hyorder.cancelRevokeProcess,
        data: {
          orderId: order.orderId
        },
        success: (me, data) => {
          this.$refs.table.refresh()
        }
      })
    },
    /**
     * 下载购销合同
     * @param record
     */
    downloadGxhtCg(record) {
      let env = process.env.NODE_ENV
      let url = "bus/export/gxht_cg"
      if (env == "development") {
        url = "../../../base/bus/export/gxht_cg"
      }
      this.http.exportExcel(this, {
        url: url,
        data: {
          orderId: record.orderId
        },
        success: function(me, data) {
        }
      })
      this.visible = false
    },
    changeHandler(form) {
      this.$emit("success", form)
    },
    processImg(order) {
      this.processInstanceId = order.processInstanceId
      this.processImageVisible = true
    }
  }
}
</script>
